package com.newland.mesdk.demo.fragment;

import java.util.List;
import java.util.concurrent.TimeUnit;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.module.common.lcd.Color;
import com.newland.mtype.module.common.lcd.DispType;
import com.newland.mtype.module.common.lcd.LCDClass;
import com.newland.mtype.module.common.lcd.LoadMenuResult;
import com.newland.mtype.module.common.lcd.PicLine;
import com.newland.mtype.module.common.lcd.Picture;
import com.newland.mtype.module.common.lcd.Point;
import com.newland.mtype.module.common.lcd.menu.Menu;
import com.newland.mtype.module.common.lcd.menu.MenuObj;
import com.newland.mtype.module.common.lcd.menu.MenuRoot;
import com.newland.mesdk.demo.interfaceimpl.LCDInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF LCD模块
 */
public class LCDFragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private LCDInterfaceImpl lcdInterfaceImpl;
	private Button btnClear, btnSetModul, btnSetPosition, btnGetPosition;
	private Button btnGetParam, btnDrawPic, btnDrawStr, btnEnableLight;
	private Button btnCloselight, btnLoadMenu, btnSetFontColor;
	private StringBuffer buffer = new StringBuffer();
	private final String ECODE_CONSUMPTION = "00001";// 消费
	private final String ECODE_REVOCATION = "00002";// 撤销
	private final String ECODE_QUERY = "00003";// 查询
	private final String ECODE_BUY = "00004";// 购物
	private final String ECODE_CONSUMPTION_WATER = "00005";// 水费
	private final String ECODE_CONSUMPTION_ELE = "00006";// 电费
	private final String ECODE_BUY_SUB = "00007";// 购物sub

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_lcd, null);
		mainActivity = (MainActivity) getActivity();
		initLCDFragmentView(view);
		return view;
	}

	public void initLCDFragmentView(View view) {
		btnClear = (Button) view.findViewById(R.id.btn_lcd_clear);
		btnClear.setOnClickListener(this);

		btnSetModul = (Button) view.findViewById(R.id.btn_lcd_setmodule);
		btnSetModul.setOnClickListener(this);

		btnSetPosition = (Button) view.findViewById(R.id.btn_lcd_setposition);
		btnSetPosition.setOnClickListener(this);

		btnGetPosition = (Button) view.findViewById(R.id.btn_lcd_getposition);
		btnGetPosition.setOnClickListener(this);

		btnGetParam = (Button) view.findViewById(R.id.btn_lcd_get_lcdparam);
		btnGetParam.setOnClickListener(this);

		btnDrawPic = (Button) view.findViewById(R.id.btn_lcd_draw_picture);
		btnDrawPic.setOnClickListener(this);

		btnDrawStr = (Button) view.findViewById(R.id.btn_lcd_draw_string);
		btnDrawStr.setOnClickListener(this);

		btnEnableLight = (Button) view.findViewById(R.id.btn_lcd_enable_light);
		btnEnableLight.setOnClickListener(this);

		btnCloselight = (Button) view.findViewById(R.id.btn_lcd_close_light);
		btnCloselight.setOnClickListener(this);

		btnLoadMenu = (Button) view.findViewById(R.id.btn_lcd_load_menu);
		btnLoadMenu.setOnClickListener(this);

		btnSetFontColor = (Button) view.findViewById(R.id.btn_lcd_set_font_cloor);
		btnSetFontColor.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			if (mainActivity.processingisLocked()) {
				mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
			} else {
				mainActivity.connectDevice();
				lcdInterfaceImpl = new LCDInterfaceImpl();
				switch (v.getId()) {
				case R.id.btn_lcd_clear:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								lcdInterfaceImpl.clearScreen();
								mainActivity.showMessage("液晶屏清屏成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("液晶屏清屏 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_lcd_getposition:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								Point point = lcdInterfaceImpl.getCursorPosition();
								int X = point.getX();
								int Y = point.getY();
								mainActivity.showMessage("LCD获得光标位置成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.showMessage("LCD光标的X坐标为：" + X + "; LCD光标的Y坐标为：" + Y + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD获得光标位置 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_lcd_draw_string:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								String words = "Hellow World!";
								lcdInterfaceImpl.clearScreen();
								lcdInterfaceImpl.drawWithinTime(words, 3000);
								mainActivity.showMessage("LCD输出字符串并在预定的时间内显示成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD输出字符串并在预定的时间内显示 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_lcd_setmodule:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								lcdInterfaceImpl.setDisplayType(DispType.NORMAL);
								mainActivity.showMessage("设置显示模式成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("设置显示模式 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				case R.id.btn_lcd_get_lcdparam:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								LCDClass lcdClass = lcdInterfaceImpl.getLCDClass();
								String mess = lcdClass.toString();
								mainActivity.showMessage("获得液晶属性成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.showMessage("液晶属性: " + mess + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("获得液晶属性 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				case R.id.btn_lcd_enable_light:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								lcdInterfaceImpl.enableBackgroundLight();
								mainActivity.showMessage("LCD启动背光成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD启动背光 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}

					}).start();

					break;

				case R.id.btn_lcd_setposition:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {

								mainActivity.processingLock();
								Point point = new Point(0, 0);
								lcdInterfaceImpl.setCursorPosition(point);
								mainActivity.showMessage("LCD设置光标位置成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD关闭背光设置光标位置 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				case R.id.btn_lcd_draw_picture:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								Point point = new Point(10, 10);
								Point point1 = new Point(20, 20);
								Color color = new Color(10);
								Picture picLine = new PicLine(point, point1, color);
								lcdInterfaceImpl.clearScreen();
								lcdInterfaceImpl.draw(picLine);
								mainActivity.showMessage("LCD画出给定图像成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD画出给定图像 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				case R.id.btn_lcd_close_light:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								lcdInterfaceImpl.disableBackgroundLight();
								mainActivity.showMessage("LCD关闭背光成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD关闭背光 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				case R.id.btn_lcd_load_menu:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								MenuRoot menu = MenuRoot.newInstance();
								Menu level1 = MenuObj.createSubListMenu(ECODE_CONSUMPTION, "消费1");
								MenuObj.createButtonMenu(level1, ECODE_CONSUMPTION, "话费充值");
								MenuObj.createButtonMenu(level1, ECODE_CONSUMPTION_WATER, "水费充值");
								MenuObj.createButtonMenu(level1, ECODE_CONSUMPTION_ELE, "电费充值");
								MenuObj.createButtonMenu(level1, ECODE_CONSUMPTION_ELE, "手机充值");
								MenuObj.createButtonMenu(level1, ECODE_CONSUMPTION_ELE, "油费充值");
								MenuObj.createButtonMenu(level1, ECODE_CONSUMPTION_ELE, "上网充值");
								menu.addSubMenu(level1);
								Menu level2 = MenuObj.createSubListMenu(ECODE_BUY, "购物");
								MenuObj.createButtonMenu(level2, ECODE_BUY_SUB, "永辉购物");
								MenuObj.createButtonMenu(level2, ECODE_BUY_SUB, "沃尔玛购物");
								MenuObj.createButtonMenu(level2, ECODE_BUY_SUB, "大利嘉城购物购物");
								MenuObj.createButtonMenu(level2, ECODE_BUY_SUB, "amazonShopping");
								MenuObj.createButtonMenu(level2, ECODE_BUY_SUB, "jingdongAnniversary");
								MenuObj.createButtonMenu(level2, ECODE_BUY_SUB, "苏宁易购online");
								menu.addSubMenu(level2);
								menu.addSubMenu(MenuObj.createButtonMenu(ECODE_REVOCATION, "撤销"));
								menu.addSubMenu(MenuObj.createButtonMenu(ECODE_QUERY, "查询"));
								List<Menu> listMenu = menu.getSubMenu();
								Log.i("me30", "***显示菜单 start : ****" + menu.toString() + "Size = " + listMenu.size());
								for (Menu m : listMenu) {
									System.out.println("Menu = " + m.toString());
								}
								LoadMenuResult loadMenuResult = lcdInterfaceImpl.loadMenu(menu, 30, TimeUnit.SECONDS);

								mainActivity.showMessage("LCD装载菜单成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.showMessage("LCD装载菜单结果：" + loadMenuResult.toString() + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD装载菜单 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;

				case R.id.btn_lcd_set_font_cloor:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								Color color = new Color(30);
								lcdInterfaceImpl.setNormalWordsColor(color);
								mainActivity.showMessage("LCD设置通用正显字体颜色成功！" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("LCD设置通用正显字体颜色 异常：" + e + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				default:
					break;
				}
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.NORMAL);
		}
	}
}
